package com.lt.test.consumer.web.thread;

import com.lt.test.api.CloudlingService;
import com.lt.test.common.util.SendMessageUtils;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class SendCloudingMessageThread implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(SendCloudingMessageThread.class);
    private String name;
    private Thread thread;
    private CloudlingService cloudlingService;
    private List<Map<String,Object>> cloudingAndUsersList ;
    private JSONObject message;
    private String type;
    private String faSongFang;
    Map<String, Object> cloudMessage;

    public SendCloudingMessageThread(String name, CloudlingService cloudlingService, List<Map<String,Object>> cloudingAndUsersList , JSONObject message , String type, String faSongFang, Map<String, Object> cloudMessage) {
        this.name = name;
        this.cloudlingService = cloudlingService;
        this.cloudingAndUsersList=cloudingAndUsersList;
        this.message=message;
        this.type=type;
        this.faSongFang=faSongFang;
        this.cloudMessage=cloudMessage;
    }

    @Override
    public void run() {
        logger.info("子线程--发送小云消息开始");
        try {
            SendMessageUtils.sendMessage(this.cloudingAndUsersList,this.message,this.type,this.faSongFang);
            this.cloudMessage.put("date", new Date());
            Boolean isSuccess = cloudlingService.insertCloudMessageRecord(this.cloudMessage);
            if (isSuccess) {
                logger.info("插入小云消息成功");
            }else {
                logger.info("插入小云消息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("子线程--发送小云抛出异常,message:"+e.getMessage());
        }
        logger.info("子线程--发送小云消息结束");
    }

    public void start() {
        if (this.thread == null) {
            this.thread = new Thread(this, this.name);
        }
        this.thread.start();
    }
}
